﻿using UnityEngine;

/// <summary>
/// 加载AssetBundle任务，包括其依赖
/// </summary>
public class BundleTask : LoadTask
{
    /// <summary>
    /// 加载依赖bundle任务
    /// </summary>
    public LoadTaskR[] dependencies;

    AssetBundleCreateRequest request;
    public int referenceCount;
    public BundleTask(AssetBundleCreateRequest request)
    {
        this.request = request;
        referenceCount = 1;
    }

    /// <summary>
    /// 加载进度
    /// </summary>
    public override float progress
    {
        get
        {
            if (isDone)
                return 1;

            if (request == null)
                return 0;

            float current = request.progress;
            float total = 1;
            if (dependencies != null)
            {
                for (int i = 0; i < dependencies.Length; ++i)
                {
                    current += dependencies[i].progress;
                    ++total;
                }
            }
            return current / total;
        }
    }

    /// <summary> 
    /// 是否加载完成，注意:只是加载完成，不代表加载成功，需要根据error是否为空判断是否加载成功
    /// </summary>
    public bool m_IsDone = false;
    public override bool isDone
    {
        get { return m_IsDone; }
    }

    /// <summary>
    /// 更新入口
    /// </summary>
    public override void Update()
    {
        if (!isDone)
        {
            if (CheckIsDone())
            {
                m_IsDone = true;
                resource = request == null ? null : request.assetBundle;
                request = null;
                OnFinish();
            }
        }
    }

    bool CheckIsDone()
    {
        if (request == null)
            return true;

        if (dependencies != null)
        {
            for (int i = 0; i < dependencies.Length; ++i)
            {
                if (!dependencies[i].isDone)
                    return false;
            }
        }

        m_IsDone = request.isDone;
        return m_IsDone; 
    }

    public void SetDone(AssetBundle bundle)
    {
        resource = bundle;
        m_IsDone = true;
    }

    string GetError()
    {
        if (dependencies != null)
        {
            for (int i = 0; i < dependencies.Length; ++i)
            {
                var error = dependencies[i].error;
                if (!string.IsNullOrEmpty(error))
                    return error;
            }
        }

        return string.Empty;
    }

    /// <summary>
    /// 加载的AssetBundle
    /// </summary>
    public AssetBundle bundle
    {
        get { return resource as AssetBundle; }
    }

    public override string error
    {
        get
        {
            return ""; 
        }
    }

    /// <summary>
    /// 设置依赖的加载任务
    /// </summary>
    public void SetDependencies(LoadTaskR[] dependencies)
    {
        this.dependencies = dependencies;
    }

    /// <summary>
    /// 销毁对象，释放资源
    /// </summary>
    public override void Destroy()
    {
        if (bundle != null)
            bundle.Unload(false);

        SetDependencies(null);
    }

    public void ReleaseBundleAssets()
    {
        if(bundle != null)
        {
            bundle.Unload(true);
        }
        SetDependencies(null);
    }
}
